//
//  SFMovableTableViewCell.m
//  TableViewFramework
//
//  Created by 支晓辉 on 16/3/25.
//  Copyright © 2016年 wishselfly. All rights reserved.
//

#import "SFMovableTableViewCell.h"

#define APPLICATION     [UIApplication sharedApplication]

#define KEY_WINDOW  APPLICATION.keyWindow

@interface SFMovableTableViewCell () <UIGestureRecognizerDelegate>

@property (weak, nonatomic) UILongPressGestureRecognizer *longPressGesture;

@end

@implementation SFMovableTableViewCell
@synthesize delegate = _delegate;

- (void)awakeFromNib {
    [super awakeFromNib];
    [self _initView];
}

- (instancetype)init {
    if (self = [super init]) {
        [self _initView];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self _initView];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        [self _initView];
    }
    return self;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self _initView];
    }
    return self;
}

#pragma mark - Public methods

- (void)initLongPressGesture:(UILongPressGestureRecognizer *)gesture {
    gesture.minimumPressDuration = 0.5f;
    gesture.numberOfTouchesRequired = 1;
    gesture.delegate = self;
    [self addGestureRecognizer:gesture];
}

#pragma mark - Private methods

/**
 *  初始化当前视图
 */
- (void)_initView {
    UILongPressGestureRecognizer *gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(dealWithLongPress:)];
    [self initLongPressGesture:gesture];
    self.longPressGesture = gesture;
    self.customAlpha = 1.f;
}

- (void)dealWithLongPress:(UILongPressGestureRecognizer*)g {
    if ([self.delegate conformsToProtocol:@protocol(SFMovableTableViewCellDelegate)]) {
        [self.delegate movableTableViewCell:self dealWithLongPress:g];
    }
}

#pragma mark - UIGestureRecognizerDelegate

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    
    CGPoint loc = [touch locationInView:KEY_WINDOW];
    UIView *dragMoveView = self.longPressGesture.view;
    if (dragMoveView && self.longPressToDrag) {
        CGRect dragMoveViewFrame = [dragMoveView.superview convertRect:dragMoveView.frame
                                                                toView:KEY_WINDOW];
        if (CGRectContainsPoint(dragMoveViewFrame, loc)) {
            return gestureRecognizer == self.longPressGesture;
        }
    }
    
    if ([super respondsToSelector:@selector(gestureRecognizer:shouldReceivePress:)]) {
        return [super gestureRecognizer:gestureRecognizer shouldReceiveTouch:touch];
    } else {
        return YES;
    }
}

#pragma mark - Getters

- (BOOL)longPressToDrag {
    return self.longPressGesture.enabled;
}

#pragma mark - Setters

- (void)setAlpha:(CGFloat)alpha {
    if (self.unlockAlpha) {
        [super setAlpha:alpha];
    } else {
        super.alpha = self.customAlpha;
    }
}

- (void)setCustomAlpha:(CGFloat)customAlpha {
    _customAlpha = customAlpha;
    [super setAlpha:customAlpha];
}

- (void)setLongPressToDrag:(BOOL)longPressToDrag {
    self.longPressGesture.enabled = longPressToDrag;
}

@end
